﻿using System.Collections.Generic;
using System.Drawing;

namespace TodoSoft.Controls
{
	public class TreeGridGroup
	{
		public TreeGridGroup()
		{
			TextColor = Color.Black;
			BorderColor = Color.LightGray;
			HeaderRow = new TreeGridHeaderRow(this);
		}
		
		public readonly List<TreeGridColumn> Columns = new List<TreeGridColumn>();
		public readonly TreeGridHeaderRow HeaderRow;
		public Font Font = new Font("Arial", 10, FontStyle.Regular);

		public Color TextColor
		{
			set
			{
				_textColor = value;
				TextBrush = new SolidBrush(_textColor);
			}
			get
			{
				return _textColor;
			}
		}

		public Color BorderColor
		{
			set
			{
				_borderColor = value;
				BorderPen = new Pen(_borderColor, 1);
			}
			get
			{
				return _borderColor;
			}
		}

		public Brush TextBrush;
		public Pen BorderPen;

		private Color _textColor;
		private Color _borderColor;

		public TreeGridColumn AddColumn(string caption, int width)
		{
			var column = new TreeGridColumn(this, caption, width);
			Columns.Add(column);
			return column;
		}

		string GetVisibilitySymbol(bool flag)
		{
			return flag ? "-" : "+";
		}

		public void AddSwitchColumn(bool defaultShowChilds)
		{
			var switchColumn = new TreeGridColumn(this, "+/-", 10);

			switchColumn.CellClick += cell =>
					{
						if (cell.Column != switchColumn) return;

						var data = cell.Row.Cells[cell.Row.Group.Columns.IndexOf(cell.Column)];
							
						var childsVisible = data == null ? defaultShowChilds : (string) data == GetVisibilitySymbol(true);

						childsVisible = !childsVisible;

						cell.Row.Cells[cell.Row.Group.Columns.IndexOf(cell.Column)]
							= GetVisibilitySymbol(childsVisible);

						cell.Row.SetChildsVisible(childsVisible);
					};

			Columns.Add(switchColumn);
		}
	}
}